package com.zhaotianhao.task;

import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.zhaotianhao.pojo.FundsTarde;
import com.zhaotianhao.service.FundsTardeService;
import com.zhaotianhao.util.AlibabapayGateWayUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Z-7
 * @Date 2022/3/5
 */
@Lazy(false)
@Component
@EnableScheduling
public class FundsTask {
    @DubboReference
    private FundsTardeService fundsTardeService;
    /**
     * 掉单处理
     */
    @Scheduled(cron = "0 * * * * ?")
    public void orderCompensator() throws AlipayApiException {
        System.out.println("开始检测");
        // 查询5分钟前未支付的订单
        List<FundsTarde> list = fundsTardeService.queryUnPay(5);
        Map<String, String> map = new HashMap<>();
        for (FundsTarde fundsTarde : list) {
            // 查询支付宝中是否有记录
            map.put("out_trade_no",fundsTarde.getId());
            AlipayTradeQueryResponse alipayTradeQueryResponse = AlibabapayGateWayUtils.queryOrder(map);
            if("TRADE_SUCCESS".equals(alipayTradeQueryResponse.getTradeStatus())){
                // 掉单 处理
                fundsTardeService.updateTardeStatus(fundsTarde.getId(),1);
                System.out.println("漏单"+fundsTarde.getId());
            }else{
                fundsTardeService.deleteById(fundsTarde.getId());
                System.out.println("超时，删除:"+fundsTarde.getId());
            }
        }
    }
}
